package com.example;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.IOException;
import java.util.List;
import java.io.PrintWriter;

public class BookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        List<Book> cart = null;

        if (session != null) {
            cart = (List<Book>) session.getAttribute("cart");
        }

        out.println("<html><body>");
        if (cart == null || cart.isEmpty()) {
            out.println("对不起！您还没有购买任何商品！");
        } else {
            out.println("您购买的书籍有：<br>");
            for (Book book : cart) {
                out.println(book.getName() + "<br>");
            }
        }
        out.println("</body></html>");
    }
}